import Vue from 'vue'
import VueRouter from 'vue-router'

// App_old 当作 xflow 作为内容主页
import xflow from "../views/App_old.vue"

// 登陆
import Login from "../views/Login.vue"
import CoreLogin from '../components/login/CoreLogin.vue'
import CoreRegister from '../components/login/CoreRegister.vue'

// 后台管理 admin
// // 管理员 利用旧的Publish组件
import admin from '../views/Admin.vue'
// import PubNote from '../components/publish/PubNote.vue'
import DataShow from '../components/admin/DataShow.vue' // 数据展示
import ManageUser from '../components/admin/ManageUser.vue' // 用户管理
import ManageSinger from '../components/admin/ManageSinger.vue' // 歌手管理



Vue.use(VueRouter)

const routes = [
  {
    path: '',
    redirect: "/login"
  },
  // 登录
  {
    path: '/login',
    component: Login,
    children: [
      {
        path: '/',
        redirect: "coreLogin"
      },
      {
        path: 'coreLogin',
        component: CoreLogin
        
      }, 
      {
        path: 'coreRegister',
        component: CoreRegister
      }
    ]
  },
  // 管理员
  {
    path: '/Admin',
    component: admin,
    children: [
      {
        path: '/',
        redirect:"DataShow"
      },
      {
        path: 'DataShow',
        component: DataShow
      },
      {
        path: 'ManageUser',
        component: ManageUser
      },
      {
        path: 'ManageSinger',
        component: ManageSinger
      }
    ]
  }
]

const router = new VueRouter({
  // mode: 'history',
  mode: 'hash',
  routes
})

// 路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    return next()
  }
  if (to.path === '/login/coreLogin') {
    return next()
  }
  if (to.path === '/login/coreRegister') {
    return next()
  }


  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) {
    return next('/login')
  }

  return next()

})

export default router

